%{
Get the calibrated and counterfactual results together
%}
clear all;

need_more_time = [];
stata_matrix_all = [];


entry_array = 1:1:120;

for model=1:3+size(entry_array,2)*2+2
  %  eval(['cd run',num2str(results),';']);
    if model==1 
         load results/resultsBaseline.mat
         saveas = 'results/Baseline_data';      
    elseif model ==2
         load results/resultsBaseline_flexcommission.mat
         saveas = 'results/Baseline_flexc_data';
         Vb = zeros(1,9);
         psi = Psi_flex;
    elseif model ==3
         load results/resultsBaselineBuyerfix_flexcommission.mat
         saveas = 'results/Baseline_flexcBF_data';
         psi = Psi_flex;
    elseif model>3 && model<=3+size(entry_array,2) % entrycost
            eval(['load results/resultsEntryCost',num2str(model-3),'.mat']);
            saveas = strcat('results/EntryCost',num2str(model-3),'_data');
    elseif model>3+size(entry_array,2)  && model <= 3+size(entry_array,2)*2
            eval(['load results/resultsEntryCost',num2str(model-(3+size(entry_array,2))),'Buyerfix.mat']);
            saveas = strcat('results/EntryCost',num2str(model-(3+size(entry_array,2))),'BF_data');
    elseif model>3+size(entry_array,2)*2 && model<=3+size(entry_array,2)*2+1 % fixed commission
             load results/resultsCommissionEffAvg.mat;
             saveas = 'results/Com_EffAvg_data';
    elseif model>3+size(entry_array,2)*2+1 && model<=3+size(entry_array,2)*2+2 % fixed commission with fixed number of buyers
             load results/resultsCommissionBuyerfixEffAvg.mat;
             saveas = 'results/Com_BFEffAvg_data';       
    end
    disp(saveas);    
    % compute seller value
    bet_client = 0.95;
    agent_match_prob = (repmat((Ne').*dist',1,kw).*w_mat) ./ repmat(sum(repmat((Ne').*dist',1,kw).*w_mat,1),ks,1); % probability of matching with each agents
    Vs = zeros(1,9);
    found=0;
    while found==0
        EVs = repmat((T*Vs')',ks,1).*w_mat;% expected future value
        Vs_new = sum(agent_match_prob.*(repmat(Sale_p,1,kw).*repmat(Price_vec',1,kw).*(1-psi-0.03) + bet_client*(1-repmat(Sale_p,1,kw)).*EVs),1);
        if max(abs((Vs_new-Vs)./Vs))<0.001
            found=1;
        end
        Vs = Vs_new;
    end
    
    Vb = zeros(1,9);
    found=0;
    gamm = 0.5;
    while found==0
        EVb = repmat((T*Vb')',ks,1).*w_mat;% expected future value
        Vb_new = sum(agent_match_prob.*(repmat(Buy_p,1,kw).*repmat(Price_vec'/gamm-Price_vec',1,kw) + bet_client*(1-repmat(Buy_p,1,kw)).*EVb),1);
        if max(abs((Vb_new-Vb)./Vb))<0.001
            found=1;
        end
        Vb = Vb_new;
    end

    % listing accumulation
    num_bs = Ne+Nb;
    b = regress(num_bs',State(1,:)');

    % need mode time
    fail = nmain==1000;

    % save parameters and listing accumulation moment
    parameters = [mu_fc sigma_fc c_e delt b fail]; % what's b? added delta 
    parameters = repmat(parameters,size(Ne,2),1);

    % save output
    if model ==2 || model==3 % efficiency benchmark
        stata_matrix = [State(1,:)' State(3,:)' (Ne)' (Nb)' Sale_p Buy_p  EProfit policy dist' lambd(State(3,:))' Vs(State(3,:))' parameters Vb(State(3,:))' Psi_flex];
    elseif model>3 && model <=3+size(entry_array,2)*2 % Entry costs
         stata_matrix = [State(1,:)' State(3,:)' (Ne)' (Nb)' Sale_p Buy_p EProfit policy dist' lambd(State(3,:))' Vs(State(3,:))' parameters Vb(State(3,:))'];
    else % Baseline
        stata_matrix = [State(1,:)' State(3,:)' (Ne)' (Nb)' Sale_p Buy_p EProfit policy dist' lambd(State(3,:))' Vs(State(3,:))' parameters Vb(State(3,:))'];
    end
    eval(['csvwrite(''',saveas,'.csv'',stata_matrix);']);
end




